﻿#include "MusicListDelegate.h"
#include<qpainter.h>
#include<qurl.h>
#include<qpixmap.h>
#include<qfont.h>
#include<QTime>
#include<qapplication.h>
MusicListDelegate::MusicListDelegate(QObject *parent)
	: QStyledItemDelegate(parent)
{}

QSize MusicListDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
	return QSize(64,64);
}

void MusicListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{

	QStyleOptionViewItem opt(option);
	opt.state &= ~QStyle::State_HasFocus; // 去除焦点状态

	QStyledItemDelegate::paint(painter, opt, index); // 用去掉焦点状态的 opt 绘制
	painter->save();

	QString songCover = index.data(Qt::UserRole + 1).toString();
	QString songName = index.data(Qt::UserRole + 2).toString();
	QString singerName = index.data(Qt::UserRole + 3).toString();
	int songDuration_ms = index.data(Qt::UserRole + 4).toInt() * 1000;
	QUrl songUrl = index.data(Qt::UserRole + 5).toUrl();
	bool itemIsPlaying = index.data(Qt::UserRole + 6).toBool();
	QColor playingColor(0, 119, 255);
	

	int num = index.row() + 1;
	QString serialNumber;
	
	if (num>=1 && num<=9)
	{
		serialNumber = "0"+QString::number(num);
	}
	else
	{
		serialNumber = QString::number(num);
	}

	QPoint serialNumberPoint{ option.rect.x() + 10, option.rect.y() + option.rect.height() / 2 + 4};
	if (itemIsPlaying)
	{
		painter->setPen(playingColor);
	}
	else
	{
		painter->setPen(QColor(134, 136, 139));
	}

	painter->drawText(serialNumberPoint, serialNumber);
	//painter->drawText(option.rect,Qt::AlignVCenter | Qt::AlignLeft,serialNumber);




	if (singerName.isEmpty()) 
	{
		int Strindex = songName.indexOf('-');
		if (Strindex != -1)
		{
			singerName = songName.right(songName.size() - Strindex - 1);
			songName = songName.left(Strindex);
		}
	}


	QPixmap cover(songCover);
	if (cover.height() >= option.rect.height())
	{
		cover = cover.scaled(50, 50);
	}
	painter->setRenderHint(QPainter::SmoothPixmapTransform);
	QPoint pixmapPoint{serialNumberPoint.x()+20,option.rect.y()+(option.rect.height() - cover.height())/2};


	painter->drawPixmap(pixmapPoint, cover);
	
	QPoint songNamePoint{ pixmapPoint.x() + cover.width() + 5
		,option.rect.y() + (option.rect.height() - cover.height() + 1) / 2 + 20};
	const QFont defaultFont = painter->font();
	const QPen defaultPen = painter->pen();
	QFont songNameFont;
	songNameFont.setFamily("等线");
	songNameFont.setPixelSize(14);
	painter->setFont(songNameFont);
	if (itemIsPlaying)
	{
		painter->setPen(playingColor);
	}
	else
	{
		painter->setPen(QColor(51, 51, 51));
	}
	
	painter->drawText(songNamePoint, songName);


	if (!singerName.isEmpty())
	{
		QPoint singerNamePoint{ songNamePoint.x(),songNamePoint.y() + 20 };
		QFont& singerNameFont = songNameFont;
		singerNameFont.setPixelSize(13);
		painter->setFont(singerNameFont);
		if (itemIsPlaying)
		{
			painter->setPen(playingColor);
		}
		else
		{
			painter->setPen(QColor(134, 136, 139));
		}
		
		painter->drawText(singerNamePoint, singerName);
	}
	
	//duration
	int h = 0;
	int min = 0;
	int s = 0;
	QTime songLength = QTime::fromMSecsSinceStartOfDay(songDuration_ms);
	QString songDuartionText;

	if (songDuration_ms >= 3600000)
	{
		songDuartionText = songLength.toString("hh:mm:ss");
	}
	else
	{
		songDuartionText = songLength.toString("mm:ss");
	}


	painter->restore();
	painter->save();
	if (itemIsPlaying)
	{
		painter->setPen(playingColor);
	}
	else
	{
		painter->setPen(QColor(134, 136, 139));
	}
	painter->drawText(option.rect.width() - 220, serialNumberPoint.y(), songDuartionText);

	//painter->setPen(defaultPen);
	//painter->setFont(defaultFont);
	painter->restore();

}

MusicListDelegate::~MusicListDelegate()
{}


